﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyCommerce.ManagementBase.Common.Base.Common;
using MyCommerce.Presentation.Orders.IViews;
using MyCommerce.Presentation.Orders.Presenters;
using Innova.Microfinanzas.BusinessEntities;
using Innova.FoundationFramework.DataTypes;

namespace MyCommerce.Windows.Orders.Views
{
    public partial class OrdersToSuppliersAdministrationView : BaseForm, IOrdersToSuppliersAdministrationView
    {
        private OrdersToSuppliersAdministrationPresenter pPresenter;

        public OrdersToSuppliersAdministrationView()
        {
            InitializeComponent();
            pPresenter = new OrdersToSuppliersAdministrationPresenter(this);
        }

        #region IOrdersToSuppliersAdministrationView Members

        public long IdStoreSelect
        {
            get
            {
                if (listStores.SelectedIndex != -1)
                    return Convert.ToInt64(listStores.SelectedValue);
                return 0;
            }
            set
            {
                if (value == 0)
                    listStores.SelectedIndex = -1;
                else if (String.IsNullOrEmpty(listStores.ValueMember) == false)
                    listStores.SelectedValue = value;
            }
        }

        public long UserId 
        {
            get
            {
                return Context.UserId;
            }
        }

        public OrderSupplier ObjOrderSelect
        {
            get 
            {
                if (gridOrders.ActiveRow != null
                    && gridOrders.ActiveRow.Selected)
                    return (OrderSupplier)gridOrders.ActiveRow.ListObject;
                return null;
            }
        }

        public List<OrderSupplier> ColOrdersLoad
        {
            set
            {
                gridOrders.DataSource = value;
                gridOrders.DataBind();
            }
        }

        public List<Store> ColStoresLoad 
        {
            set
            {
                listStores.DisplayMember = "Name";
                listStores.ValueMember = "IdStore";
                listStores.DataSource = value;
            }
        }

        public bool InvokeNewOrderForm()
        {
            bool result = false;
            Command commandForm = Context.GetCommand("NewOrder");
            object formResult = Context.InvoqueCommandWithParams(commandForm, IdStoreSelect, true);
            if (formResult != null)
                result = Convert.ToBoolean(formResult);
            return result;
        }

        public bool InvokeViewMode(long idOrderToLoad, bool isViewMode)
        {
            bool result = false;
            string commandKey = isViewMode ? "ViewOrder" : "DeliveryOrder";
            Command commandForm = Context.GetCommand(commandKey);
            object formResult = Context.InvoqueCommandWithParams(commandForm, idOrderToLoad, true);
            if (formResult != null)
                result = Convert.ToBoolean(formResult);
            return result;
        }

        public event GenericEventHandler<long> OnLoadView;

        public event EventHandler OnViewDetail;

        public event EventHandler OnNewOrder;

        public event EventHandler OnCancelOrder;

        public event EventHandler OnLoadOrders;

        public event EventHandler OnDeliveryOrder;

        #endregion

        private void tsCloseButtonToolStrip_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsDeleteButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (ObjOrderSelect != null)
            {
                if (Context.ShowQuestionMessageBox("¿Desea anular el pedido?", "Anular pedidos") == System.Windows.Forms.DialogResult.Yes)
                {
                    if (OnCancelOrder != null)
                    {
                        OnCancelOrder(this, new EventArgs());
                        Context.ShowInformationMessageBox("El pedido se anuló correctamente.", "Anular pedidos");
                    }
                }
            }
            else
                Context.ShowInformationMessageBox("Debe seleccionar un pedido para poder anularlo.", "Anular pedidos");
        }

        private void tsNewButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (IdStoreSelect != 0)
            {
                if (OnNewOrder != null)
                    OnNewOrder(this, new EventArgs());
            }
            else
                Context.ShowInformationMessageBox("Debe seleccionar un almacén para poder realizar el pedidos.", "Nueva solicitud de pedido");
        }

        private void tsSearchButtonToolStrip_Click(object sender, EventArgs e)
        {
            if (ObjOrderSelect != null)
            {
                if (OnViewDetail != null)
                {
                    OnViewDetail(this, new EventArgs());
                }
            }
            else
                Context.ShowInformationMessageBox("Debe seleccionar un pedido para poder visualizarlo.", "Detalle pedidos");
        }

        private void OrdersToSuppliersAdministrationView_Load(object sender, EventArgs e)
        {
            if (Context.StoreId != 0)
                listStores.Enabled = false;

            if (OnLoadView != null)
                OnLoadView(this, new GenericEventArgs<long>(Context.StoreId));
        }

        private void btnFiltrar_Click(object sender, EventArgs e)
        {
            if (IdStoreSelect != 0)
            {
                if (OnLoadOrders != null)
                    OnLoadOrders(this, new EventArgs());
            }
            else
                Context.ShowInformationMessageBox("Debe seleccionar un almacén para poder cargar los pedidos.", "Administración de pedidos");
        }

        private void tsDeliveryOrder_Click(object sender, EventArgs e)
        {
            if (ObjOrderSelect != null)
            {
                if (OnDeliveryOrder != null)
                {
                    OnDeliveryOrder(this, new EventArgs());
                }
            }
            else
                Context.ShowInformationMessageBox("Debe seleccionar un pedido para poder registrar la entrega.", "Detalle pedidos");
        }
    }
}
